package com.hua.fanqiemall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.alibaba.fastjson.JSONObject;
import com.hua.fanqiemall.product.entity.AttrEntity;
import com.hua.fanqiemall.product.service.AttrAttrgroupRelationService;
import com.hua.fanqiemall.product.service.AttrService;
import com.hua.fanqiemall.product.service.CategoryService;
import com.hua.fanqiemall.product.vo.AttrGroupRelationReqVo;
import com.hua.fanqiemall.product.vo.AttrGroupWithAttrsRspVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.hua.fanqiemall.product.entity.AttrGroupEntity;
import com.hua.fanqiemall.product.service.AttrGroupService;
import com.hua.common.utils.PageUtils;
import com.hua.common.utils.R;



/**
 * 属性分组
 *
 * @author luoweihua
 * @email lwh_559@163.com
 * @date 2021-12-09 22:14:00
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService relationService;

    private static final Logger logger = LoggerFactory.getLogger(AttrGroupController.class);

    /**
     * 添加属性与属性分组关联关系接口
     * @author luoweihua
     * @date 2022/1/12 9:26
     * @param reqVoList 入参
     * @return R
     */
    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody List<AttrGroupRelationReqVo> reqVoList){
        relationService.saveBatch(reqVoList);
        return R.ok();
    }
    /**
     * 删除属性分组关联接口
     * @author luoweihua
     * @date 2022/1/11 16:40
     * @param reqVoListVo 删除列表
     * @return R
     */
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody List<AttrGroupRelationReqVo> reqVoListVo){
        logger.info("删除属性分组关联接口入参:{}", JSONObject.toJSONString(reqVoListVo));
        attrService.deleteRelation(reqVoListVo);
        return R.ok();
    }

    /**
     * 获取属性分组的关联信息接口
     * @author luoweihua
     * @date 2022/1/11 16:23
     * @param attrGroupId 分组ID
     * @return R
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrGroupId){
        logger.info("获取属性分组的关联信息接口入参:{}", attrGroupId);
        List<AttrEntity> attrEntityList = attrService.getRelationAttr(attrGroupId);
        return R.ok().put("data", attrEntityList);
    }
    /**
     * 获取属性分组没有关联的信息
     * @author luoweihua
     * @date 2022/1/11 21:45
     * @param params
     * @param attrGroupId
     * @return R
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params , @PathVariable("attrgroupId") Long attrGroupId){
        PageUtils page = attrService.getNoRelationAttr(params , attrGroupId);
        return R.ok().put("page", page);
    }

    @GetMapping("{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupWithAttrsRspVo> rspVoList = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",rspVoList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catalogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params , @PathVariable("catalogId") Long catalogId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catalogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] catelogPath = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
